package com.yulan.bankback.service.bank.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yulan.bankback.entity.model.bank.BankUser;
import com.yulan.bankback.entity.model.bank.RiskEvent;
import com.yulan.bankback.entity.dto.bank.*;
import com.yulan.bankback.entity.event.LoginEvent;
import com.yulan.bankback.entity.vo.SafeVo;
import com.yulan.bankback.mapper.bank.BankUserMapper;
import com.yulan.bankback.mapper.bank.LoginEventMapper;
import com.yulan.bankback.mapper.bank.NoticeMapper;
import com.yulan.bankback.mapper.bank.RiskEventMapper;
import com.yulan.bankback.service.bank.SafeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


@Service
public class SafeServiceImpl implements SafeService {

    @Resource
    private RiskEventMapper riskEventMapper;

    @Resource
    private BankUserMapper bankUserMapper;

    @Resource
    private LoginEventMapper loginEventMapper;

    @Resource
    private NoticeMapper noticeMapper;


    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");


    @Override
    public SafeVo getUserSafeInfo(String uid) {
        BankUser user = bankUserMapper.selectById(uid);
        if(user == null) return null;

        SafeVo safeVo = new SafeVo();

        safeVo.setSafeScore(user.getSafeScore());
        List<LoginDateDto> loginMonthEvents
                = loginEventMapper.getGroupLoginDateInterval(uid, "1 month");

        List<String> loginMonthDate = new ArrayList<>();
        List<Integer> loginMonthData = new ArrayList<>();

        loginMonthEvents.forEach(item -> {
            loginMonthDate.add(item.getDate());
            loginMonthData.add(item.getCount());
        });

        safeVo.setLoginMonthDate(loginMonthDate);
        safeVo.setLoginMonthData(loginMonthData);

        List<LoginTypeDto> loginMonthTypes
                = loginEventMapper.getGroupLoginTypeInterval(uid, "1 month");
        List<PieChartDto<Integer>> loginTypeDistribute = new ArrayList<>();
        loginMonthTypes.forEach(item ->{
            loginTypeDistribute.add(new PieChartDto<>(
                    item.getCount(),
                    item.getType()));
        });
        safeVo.setLoginTypeDistribute(loginTypeDistribute);

        List<LoginPlaceDto> loginMonthPlace
                = loginEventMapper.getGroupLoginPlaceInterval(uid, "1 month");
        List<PieChartDto<Integer>> loginPlaceDistribute = new ArrayList<>();
        loginMonthPlace.forEach(item ->{
            loginPlaceDistribute.add(new PieChartDto<>(
                    item.getCount(),
                    item.getPlace()));
        });
        safeVo.setLoginPlaceDistribute(loginPlaceDistribute);


        List<LoginEvent> loginEvents
                = loginEventMapper.getLastMonthEvent(uid);
        loginEvents.forEach(item -> item.setViewLoginDate(formatter2.format(item.getLoginDate())));

        safeVo.setLoginEvents(loginEvents);

        List<RiskEvent> remoteLogins = riskEventMapper.selectList(
                new QueryWrapper<RiskEvent>()
                        .eq("uid", uid)
                        .eq("type", "remote_login"));

        List<RiskEvent> violationTradings = riskEventMapper.selectList(
                new QueryWrapper<RiskEvent>()
                        .eq("uid", uid)
                        .eq("type", "violation_trading"));

        safeVo.setRemoteLogins(remoteLogins);
        safeVo.setViolationTradings(violationTradings);

        return safeVo;
    }

    @Override
    public List<LoginEvent> getEventInterval(String uid,  String type, String start, String end) {
        BankUser user = bankUserMapper.selectById(uid);
        if(user == null) return null;

        QueryWrapper<LoginEvent> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("uid", uid)
                .gt("login_date", start)
                .lt("login_date",end);
        if(StringUtils.isNotBlank(type))
            queryWrapper.eq("login_type", type);

        List<LoginEvent> loginEvents = loginEventMapper.selectList(queryWrapper);

        loginEvents.forEach(e -> e.setViewLoginDate(formatter2.format(e.getLoginDate())));
        return loginEvents;
    }

    @Override
    public List<NoticeDto> getAllSafeNoticesId() {
        List<NoticeDto> res = noticeMapper.getAllSafeNoticeIds();
        res.forEach(item -> item.setTime(formatter2.format(item.getCreateTime())));
        return res;
    }


}
